package org.ruoyi.dhuman.service.impl;

import org.ruoyi.common.core.utils.MapstructUtils;
    import org.ruoyi.core.page.TableDataInfo;
    import org.ruoyi.core.page.PageQuery;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.ruoyi.dhuman.domain.bo.DhumanBo;
import org.ruoyi.dhuman.domain.vo.DhumanVo;
import org.ruoyi.dhuman.domain.Dhuman;
import org.ruoyi.dhuman.mapper.DhumanMapper;
import org.ruoyi.dhuman.service.IDhumanService;
import org.ruoyi.common.core.utils.StringUtils;

import java.util.List;
import java.util.Collection;

/**
 * 数字人Service业务层处理
 *
 * @author wangliang
 * @date Tue Oct 14 12:06:06 CST 2025
 */
@RequiredArgsConstructor
@Service
public class DhumanServiceImpl implements IDhumanService {

    private final DhumanMapper baseMapper;

    /**
     * 查询数字人
     */
    @Override
    public DhumanVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

	/**
	 * 查询数字人列表
	 */
	@Override
	public TableDataInfo<DhumanVo> queryPageList(DhumanBo bo, PageQuery pageQuery) {
		LambdaQueryWrapper<Dhuman> lqw = buildQueryWrapper(bo);
		Page<DhumanVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
		return TableDataInfo.build(result);
	}

    /**
     * 查询数字人列表
     */
    @Override
    public List<DhumanVo> queryList(DhumanBo bo) {
        LambdaQueryWrapper<Dhuman> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Dhuman> buildQueryWrapper(DhumanBo bo) {
        LambdaQueryWrapper<Dhuman> lqw = Wrappers.lambdaQuery();
                    lqw.eq(bo.getId() != null, Dhuman::getId, bo.getId());
                    lqw.eq(StringUtils.isNotBlank(bo.getTenantId()), Dhuman::getTenantId, bo.getTenantId());
                    lqw.eq(bo.getUserId() != null, Dhuman::getUserId, bo.getUserId());
                    lqw.eq(StringUtils.isNotBlank(bo.getImageUrl()), Dhuman::getImageUrl, bo.getImageUrl());
                    lqw.eq(StringUtils.isNotBlank(bo.getVoiceName()), Dhuman::getVoiceName, bo.getVoiceName());
                    lqw.eq(StringUtils.isNotBlank(bo.getMaterialUrl()), Dhuman::getMaterialUrl, bo.getMaterialUrl());
                    lqw.eq(bo.getCreateBy() != null, Dhuman::getCreateBy, bo.getCreateBy());
                    lqw.eq(bo.getCreateTime() != null, Dhuman::getCreateTime, bo.getCreateTime());
                    lqw.eq(bo.getUpdateBy() != null, Dhuman::getUpdateBy, bo.getUpdateBy());
                    lqw.eq(bo.getUpdateTime() != null, Dhuman::getUpdateTime, bo.getUpdateTime());
                    lqw.eq(StringUtils.isNotBlank(bo.getRemark()), Dhuman::getRemark, bo.getRemark());
        return lqw;
    }

    /**
     * 新增数字人
     */
    @Override
    public Boolean insertByBo(DhumanBo bo) {
        Dhuman add = MapstructUtils.convert(bo, Dhuman. class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改数字人
     */
    @Override
    public Boolean updateByBo(DhumanBo bo) {
        Dhuman update = MapstructUtils.convert(bo, Dhuman. class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Dhuman entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除数字人
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
